<template>
  <div>
    <el-form :model="form" :rules="rules" label-width="70px">
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="form.mobile"
          type="mobile"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
    </el-form>
    <div>
      <!-- 全局验证 -->
      <h4>全局验证</h4>
      <el-form ref="form4" :model="form" :rules="rules" label-width="70px">
        <el-form-item label="密码" prop="pwd">
          <el-input
            v-model="form.pwd"
            type="password"
            show-password
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="form.mobile"
            type="mobile"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 局部验证-->
      <h4>局部验证</h4>
      <el-form ref="form5" :model="form" :rules="rules" label-width="70px">
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="form.mobile"
            type="mobile"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="密码" prop="code">
          <el-input v-model="form.code" placeholder="验证码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getCode">获取验证码</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-button :loading="true">加载</el-button>
    <div class="box" v-loading="loading"></div>
    <el-button :loading="loading" @click="loading=true">点击获取内容</el-button>
    <el-button @click="loading=false">点击取消获取</el-button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        mobile: '',
        pwd: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, trigger: 'blue', message: '请输入手机号' },
          {
            validator: function (rule, value, callback) {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('手机号格式不正确'))
            }
          }
        ],
        pwd: [
          { required: true, trigger: 'blue', message: '请输入手机号' },
          { min: 5, max: 10, message: '长度在5~10之间', trigger: 'blur' }
        ],
        code: []
      },
      loading: false
    }
  },
  methods: {
    submitForm () {
      this.$refs.form4.validate(valid => {
        console.log(valid)
      })
    },
    getCode () {
      this.$refs.form5.validateField('mobile', valid => {
        // console.log(valid);
        if (!valid) {
          console.log('验证成功')
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.box {
  width: 800px;
  height: 500px;
  background-color: pink;
  margin: 0 auto;
}
</style>
